package com.mchina.myMaven.rest.controller;

import java.io.StringReader;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import com.mchina.myMaven.rest.bean.Employee;
import com.mchina.myMaven.rest.bean.EmployeeList;
import com.mchina.myMaven.rest.ds.EmployeeDS;
import com.mchina.myMaven.rest.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class EmployeeController {
    @Resource
    private EmployeeDS employeeDS;
    @Autowired
    private EmployeeService employeeService;

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public void setEmployeeDS(EmployeeDS ds) {
        this.employeeDS = ds;
    }

    @Resource
    private Jaxb2Marshaller jaxb2Mashaller;

    public void setJaxb2Mashaller(Jaxb2Marshaller jaxb2Mashaller) {
        this.jaxb2Mashaller = jaxb2Mashaller;
    }

    private static final String XML_VIEW_NAME = "employees";

    @RequestMapping(method = RequestMethod.GET, value = "/employee/{id}")
    public ModelAndView getEmployee(@PathVariable String id) {
        Employee e = null;
        try {
            e = this.getEmployeeService().findById(Long.parseLong(id));
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return new ModelAndView(XML_VIEW_NAME, "object", e);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/employee/{id}")
    public ModelAndView updateEmployee(@RequestBody String body) {
        Source source = new StreamSource(new StringReader(body));
        Employee e = (Employee) jaxb2Mashaller.unmarshal(source);
        //
        this.getEmployeeService().update(e);
        return new ModelAndView(XML_VIEW_NAME, "object", e);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/employee")
    public ModelAndView addEmployee(@RequestBody String body) {
        Source source = new StreamSource(new StringReader(body));
        Employee e = (Employee) jaxb2Mashaller.unmarshal(source);
        //employeeDS.add(e);
        try {
            this.getEmployeeService().save(e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return new ModelAndView(XML_VIEW_NAME, "object", e);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/employee/{id}")
    public ModelAndView removeEmployee(@PathVariable String id) {
        // employeeDS.remove(Long.parseLong(id));
        this.getEmployeeService().deleteById(Long.parseLong(id));
        List<Employee> employees = this.getEmployeeService().findAll();
        EmployeeList list = new EmployeeList(employees);
        return new ModelAndView(XML_VIEW_NAME, "employees", list);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/employees")
    public ModelAndView getEmployees() {

        List<Employee> employees = this.getEmployeeService().findAll();
        System.out.println(employees.toString());
        EmployeeList list = new EmployeeList(employees);
        return new ModelAndView(XML_VIEW_NAME, "employees", list);
    }

}
